package vikaa.timelinepackage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import vikaa.tools.BitmapManager;
import vikaa.tools.HttpUtil;
import vikaa.tools.ValueClass;
import vikaa.wowo.R;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterCommentItem extends BaseAdapter implements 
		OnScrollListener {

	private LayoutInflater inflater;
	private int lastItem = 0;
	private ArrayList<Comment_item> tlitem;
	private Context context;
	private int visibleLastIndex = 0;  
    private int visibleItemCount;       
    private int count=0;
    private int first = 0;
    public static boolean ran = true;
    private BitmapManager bitmapManager;

	public AdapterCommentItem(final LayoutInflater inflater, ArrayList<Comment_item> tlitem,Context context) {
		this.inflater = inflater;
		this.tlitem =  tlitem;
		this.context = context;
		this.bitmapManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_dface));
	}

	public int getCount() {
		return tlitem.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder wh;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.timeline_comment_list_item, null);
			wh = new Holder();
			wh.avater = (ImageView) convertView.findViewById(R.id.header_avater);
	        wh.nickname = (TextView) convertView.findViewById(R.id.header_nickname);
	        wh.created_at = (TextView) convertView.findViewById(R.id.header_create);
	        wh.text =(TextView)convertView.findViewById(R.id.header_text);
	        convertView.setTag(wh);
		}else{
			wh = (Holder)convertView.getTag();
		}
		Comment_item item = tlitem.get(position);
        wh.nickname.setText(item.getNickname());
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(Long.valueOf(item.getCreated_at())*1000);
        wh.created_at.setText(sdf.format(date));
        wh.text.setText(item.getText()/*StringUtils.ToSBC(item.getText())*/);
        TextPaint tp = wh.nickname.getPaint(); 
        tp.setFakeBoldText(true);
        //load avater
        if(item.getAvater().length()<5){
        	wh.avater.setBackgroundResource(R.drawable.wowo_item_cover);
        }else{
        	AsyLoadImg.loadCornerAvater(HttpUtil.HttpUrlComplete(item.getAvater()), wh.avater,ValueClass.CORNER_PIXEL);
        	//bitmapManager.loadBitmap(HttpUtil.HttpUrlComplete(item.getAvater()), wh.avater , true , ValueClass.CORNER_PIXEL);
        }
		return convertView;
	}
	static class Holder {
    	public ImageView avater = null;
		public TextView nickname = null;
		public TextView created_at = null;
		public TextView text = null;
    }
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.first = firstVisibleItem;
		if(first != 0)first--;
		this.visibleItemCount = visibleItemCount;  
        visibleLastIndex = first + visibleItemCount -2; 
        count = totalItemCount;
        
	}
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = this.getCount()-1;      
        int lastIndex = itemsLastIndex ;  
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE  
                && visibleLastIndex == lastIndex && ran && (count-1) < TimelineComment.count) {  
        	TimelineComment.page++;
        	TimelineComment.tlc.new CommentThread(String.valueOf(TimelineComment.page), TimelineComment.hash, 2).start() ;
        	ran = false;
        }  
	}
	
}
